package com.css.fxfzmh.modules.templateManage.repository.impl;

import com.css.fxfzmh.constants.YNEnum;
import com.css.fxfzmh.modules.templateManage.entity.Template;
import com.css.fxfzmh.modules.templateManage.param.TemplateParam;
import com.css.fxfzmh.modules.templateManage.repository.TemplateRepository;
import com.css.fxfzmh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/10
 * @Description: 图件模板
 */
@Repository
public class TemplateRepositoryImpl implements TemplateRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "mh_template_manage";

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, TemplateParam param) {
        StringBuilder sql = new StringBuilder("select * from " + TABLE_NAME);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //时间范围
        if (!PlatformObjectUtils.isEmpty(param.getStartTime()) & !PlatformObjectUtils.isEmpty(param.getEndTime())) {
            whereSql.append(" and create_time between '" + param.getStartTime() + "' and '" + param.getEndTime() + "'");
        }
        //大于开始时间
        if (!PlatformObjectUtils.isEmpty(param.getStartTime()) & PlatformObjectUtils.isEmpty(param.getEndTime())) {
            whereSql.append(" and create_time  >='" + param.getStartTime() + "'");
        }
        //小于结束时间
        if ( !PlatformObjectUtils.isEmpty(param.getEndTime()) & PlatformObjectUtils.isEmpty(param.getStartTime())) {
            whereSql.append(" and create_time  <='" + param.getEndTime() + "'");
        }
        //模板名称
        if (!PlatformObjectUtils.isEmpty(param.getTemplateName())) {
            whereSql.append(" and template_name like '%" + param.getTemplateName() + "%'");
        }
        //上传人
        if (!PlatformObjectUtils.isEmpty(param.getUserName())) {
            whereSql.append(" and user_name like '%" + param.getUserName() + "%'");
        }
        sql.append(whereSql).append(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Template.class)).size();
        List<Template> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(Template.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void save(Template template) {
        String sql="insert into "+TABLE_NAME+"(id,attach_id,template_name,template_desc,create_user,create_time,del_flag,user_name) " +
                "values(:id,:attachId,:templateName,:templateDesc, :createUser, :createTime,:delFlag,:userName)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(template);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delete(List<String> asList) {
        String sql = "update "+TABLE_NAME+" set del_flag = '"+ YNEnum.Y.toString()+"' where id in  ('" + String.join("','", asList) + "')";
        jdbcTemplate.update(sql);
    }
}
